package 上课案例.untitled.src.com.isoft.controller;

import com.isoft.pojo.Save;
import com.isoft.view.BankSystemMenu;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.DoubleToIntFunction;

/**
 * @Author:吴
 * @Date:2024/8/28 14:33
 * @Description:使用集合完成数据存取功能
 **/
public class BankSystem {
    int o;
    HashMap<String, Save> bankdb;

    Scanner sc;

    public BankSystem() throws Exception {
        bankdb = new HashMap<>();
        sc = new Scanner(System.in);
        initBankDB();
    }
    public void initBankDB() throws Exception {

        File file=new File("untitled/src/main/java/com/itheima/上课案例/untitled/src/com/isoft/db/AccountInfo.txt");
        FileReader reader=new FileReader(file);

        BufferedReader br=new BufferedReader(reader);
        String str="";
        while((str= br.readLine())!=null){
            String[] save_arr=  str.split(",");
            Save save=new Save();
            save.setAccountid(save_arr[0]);
            save.setName(save_arr[1]);
            save.setPwd(save_arr[2]);
            save.setGender(save_arr[3]);
            save.setMoney(Double.parseDouble(save_arr[4]));
            bankdb.put(save_arr[0],save);
            Thread.sleep(1000);
            System.out.println(save);
        }
        System.out.println("账户初始化信息完毕");

    }
    public void runBankSystem() throws Exception {
        String flags = "N";
        do {
            BankSystemMenu.showMenu();
            String userInput = sc.next();
            switch (userInput) {
                case "1" -> {
                    createAccount();
                }
                case "2" -> {
                    deleteAccount();
                }
                case "3" -> {
                    saveMoney();
                }
                case "4" -> {
                    getMoney();
                }
                case "5" -> {
                    changeMony();
                }
                case "6" -> {
                    showBalance();
                }
                case "7" -> {
                    exit();
                }
                default -> {
                    System.out.println("您输入有误");
                }
            }
//            System.out.println("按回车键继续。。。");
//            sc.next();
            System.out.println("是否继续y|n");
            flags = sc.next();
            BankSystemMenu.clearScreen();
//        }while (true);

        } while (flags.equalsIgnoreCase("y"));
        System.out.println("欢迎下次再来");
    }


    public void createAccount() {

        System.out.println("创建账户");
        System.out.println("请输入用户名");
        String name = sc.next();
        System.out.println("请输入密码");
        String pwd = sc.next();
        System.out.println("请输入性别");
        String gender = sc.next();

        Save save = new Save();
        String accountid = "6626" + (int) (1000 + (Math.random() * 9000));
        System.out.println("已为您生成一个专属卡号" + accountid);
        save.setAccountid(accountid);
        save.setGender(gender);

        save.setName(name);
        save.setPwd(pwd);
        bankdb.put(accountid, save);
        System.out.println("您已经开户完成");
        System.out.println("您的开户信息" + bankdb);
    }

    public void saveMoney() throws Exception {
        System.out.println("存款");

        System.out.println("---------------");
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行存钱");
        System.out.println("---------------");
        System.out.println("请输入银行卡号");
        String account=sc.next();
        if(bankdb.containsKey(account)){
            Save save =bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd=sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入存款金额");
                    double money=sc.nextDouble();
                    save.setMoney(money+save.getMoney());
                    bankdb.put(account,save);
                    System.out.println("存款成功");
                    System.err.printf("您的卡号%s:您的姓名%s,您的余额%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }
                else {
                    count--;
                    if (count==0) {
                        System.out.println("密码次数已经用完");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("密码有误,还剩下，"+count+"请重试");
                    iscontinue=true;
                }

            }while (iscontinue);
        }else {
            System.out.println("您输入银行卡号有误,请重试");
        }

    }

    public void getMoney() throws Exception {
        System.out.println("取款");

        System.out.println("---------------");
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行取款");
        System.out.println("---------------");
        System.out.println("请输入银行卡号");
        String account=sc.next();
        if(bankdb.containsKey(account)){
            Save save =bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd=sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("请输入取款金额");
                    double money=sc.nextDouble();
                    if(money> save.getMoney()){
                        System.out.println("余额不足");
                        return;
                    }else{
                        save.setMoney(save.getMoney()-money);
                        bankdb.put(account,save);
                    }
//                    save.setMoney(money+save.getMoney());
//                    bankdb.put(account,save);
                    System.out.println("取款成功");
                    System.err.printf("您的卡号%s:您的姓名%s,您的余额%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }
                else {
                    count--;
                    if (count==0) {
                        System.out.println("密码次数已经用完");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("密码有误,还剩下，"+count+"请重试");
                    iscontinue=true;
                }

            }while (iscontinue);
        }else {
            System.out.println("您输入银行卡号有误,请重试");
        }
    }

    private void changeMony() {
        System.out.println("转账");
    }

    private void exit() {
        System.out.println("是否真的要退出系统？（Y|N）");
        String userInput=sc.next();
        if(userInput.equalsIgnoreCase("y")) {
            System.exit(0);
        }

    }

    public void showBalance() throws Exception {
        System.out.println("查询余额");
        System.out.println("---------------");
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行查询");
        System.out.println("---------------");
        System.out.println("请输入银行卡号");
        String account=sc.next();
        if(bankdb.containsKey(account)){
            Save save =bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd=sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在查询，请稍候");
                    Thread.sleep(1000);
                    System.err.printf("您的卡号%s:您的姓名%s,您的余额%f\n",save.getAccountid(),save.getName(),save.getMoney());
                    iscontinue=false;
                }
                else {
                    count--;
                    if (count==0) {
                        System.out.println("密码次数已经用完");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("密码有误,还剩下，"+count+"请重试");
                    iscontinue=true;
                }

            }while (iscontinue);
        }else {
            System.out.println("您输入银行卡号有误,请重试");
        }
    }

    public void deleteAccount() throws Exception {
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行销户");
        System.out.println("---------------");
        System.out.println("请输入银行卡号");
        String account=sc.next();
        if(bankdb.containsKey(account)){
            Save save =bankdb.get(account);
            do{
                System.out.println("请输入您的密码：");
                String pwd=sc.next();
                if(pwd.equals(save.getPwd())){
                    System.out.println("正在销户，请稍候");
                    Thread.sleep(1000);
                    bankdb.remove(account);
                    System.out.println("销户完成");
                    iscontinue=false;
                }
                else {
                    count--;
                    if (count==0) {
                        System.out.println("密码次数已经用完");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("密码有误,还剩下，"+count+"请重试");
                    iscontinue=true;
                }

            }while (iscontinue);
        }else {
            System.out.println("您输入银行卡号有误,请重试");
        }
    }

}
